package com.qf.smartplatform.security.service;

import com.qf.smartplatform.pojo.BaseUser;
import com.qf.smartplatform.pojo.User;
import com.qf.smartplatform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * projectName:day12_27_Test
 * author:guoLei
 * time:2022/1/9 17:37
 * description:
 */
@Service
public class MyUserDetailService implements UserDetailsService {
    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User userByUserName = userService.findUserByUserName(username);
        if (userByUserName == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        ArrayList<GrantedAuthority>grantedAuthorityArrayList=new ArrayList<>();
        return new BaseUser(userByUserName.getUId(),username, userByUserName.getPassword(),grantedAuthorityArrayList);
    }
}
